//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.persistence;

import ar.com.ford.it.webengine.settings.AppSettings;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Transaction;

public class PersistenceUtil {

	private static final ThreadLocal persistenceManager = new ThreadLocal();
	private static final ThreadLocal persistenceTransaction = new ThreadLocal();
	private static boolean isActivate = AppSettings.getInstance().isFjf_persistence_facilities();

	
	public static PersistenceManager getPersistenceManager() throws PersistenceException {
		if (isActivate) {
			PersistenceManager pm = (PersistenceManager)persistenceManager.get(); 
			if (pm == null)
				pm = PersistenceManagerProviderFactory.getPersistenceProvider().getManager();
			
			persistenceManager.set(pm);
			return pm;
		}
		return null;
	}
	
	
	public static Transaction newTransaction() throws PersistenceException {
		if (isActivate) {
			
			Transaction tx = (Transaction)persistenceTransaction.get();
			if (tx != null)
				return tx;
			
			PersistenceManager pm = getPersistenceManager();
			if (pm != null) {
				tx = pm.newTransaction();
				persistenceTransaction.set(tx);
			}
			
			return tx;
		}
		return null;
	}

	public static Transaction getTransaction() throws PersistenceException {
		if (isActivate) {
			
			Transaction tx = (Transaction)persistenceTransaction.get();
			if (tx == null)
				throw new PersistenceException("No active transaction");
			
			return tx;
		}
		return null;
	}
	
	public static void commitTransaction() throws PersistenceException {
		if (isActivate) {
			Transaction tx = (Transaction)persistenceTransaction.get();
			if (tx != null) {
				tx.commit();
				persistenceTransaction.set(null);
			}
		}
	}
	
	public static void rollbackTransaction() throws PersistenceException {
		if (isActivate) {
			Transaction tx = (Transaction)persistenceTransaction.get();
			if (tx != null) {
				tx.rollback();
				persistenceTransaction.set(null);
			}
		}
	}
	
	public static void closePersistenceManager() throws PersistenceException {
		if (isActivate) {
			Transaction tx = (Transaction)persistenceTransaction.get();
			if (tx != null)
				rollbackTransaction();

			PersistenceManager pm = getPersistenceManager();
			if (pm != null) {
				pm.close();
				persistenceManager.set(null);
			}
		}
	}
	
}
